package com.erdos.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class UserUtil {   

	    private static final Log logger = LogFactory.getLog(UserUtil.class);

	    private static final ThreadLocal threadUser = new ThreadLocal();
	    
	    private static Set roles = null;
	    
	    public static Long getUserID(){
	        return getThreadUser();
	    }
	    public static void setThreadUser(Long user){
	        threadUser.set(user); 
	    }
	    public static Long getThreadUser(){
	    	Long userName = (Long)threadUser.get();
	        if (userName==null) userName=new Long(-1);
	        return userName;
	        //return "admin";
	    }
	    
	    
	    public static String generatePassword(){
	    	Random random = new Random();
	    	final char[] charArray = {
	    		'0','1','2','3','4','5','6','7','8','9',
	    		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
	    		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
	    	};
	    	
	        StringBuffer password = new StringBuffer();
	        int index = 0;
	        int randomNum = 0;
	        for(int i=0; i<10; i++){
	        	randomNum = random.nextInt();
	        	if(randomNum<0){
	        		randomNum = - randomNum;
	        	}
	        	index = randomNum % charArray.length;
	        	password.append(charArray[index] );
	        }
	        return password.toString();
	    }
	    @SuppressWarnings("unchecked")
	    /**
	     * get all roles defined in the web.xml
	     * 
	     * @param InputStream in - stream type of web.xml file
	     * @return Set of role name
	     * @throws ParserConfigurationException, FactoryConfigurationError, SAXException, IOException
	     */
	    public static synchronized Set getRoles(InputStream in) throws ParserConfigurationException, 
	            FactoryConfigurationError, SAXException, IOException{
	        
	        if(roles == null){
	            DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	            Document doc = docBuilder.parse(in);
	            NodeList nodeList = doc.getElementsByTagName("role-name");
	            logger.debug("length of nodeList"+nodeList.getLength());
	            
	            roles = new HashSet();
	            for(int i=0; i<nodeList.getLength(); i++){
	                Node node = nodeList.item(i);
	                String roleName = node.getFirstChild().getNodeValue();
	                logger.debug("node is: "+roleName);
	                roles.add(roleName);
	            }
	            return roles;
	        }else{
	            return roles;
	        }
	        
	    }
}

